package demo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import dao.imp.WorkDaoImp;
import pojo.WorkMen;

import common.exception.transaction.BeanFactory;

import dao.IWorkDao;
/**
 * 业务员登陆实现
 * @author KAdmin
 */
public class Salesmen  implements KeyListener{
	private JFrame frame;
	private Container contentPane;

	private JButton button1;
	private JButton button2;
	private JLabel label1;
	private JLabel label2;
	private JTextField jText1;
	private JPasswordField jpassword;
	private int id;
	private KeyListener keyListener;
	ProcessClient p=new ProcessClient();



	public Salesmen() {
		frame = new JFrame("业务员登录");
		setUpComponent();
		setUpListener();
		frame.setBounds(500, 300, 500, 300);
		frame.setIconImage(Toolkit.getDefaultToolkit().createImage("res/bank_of_china.png"));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

	private void setUpListener() {
		button1.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				login_method();

			}

		});
		//如果在弹出框点击"退出"，则退出此系统
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}

		});


	}

	private void close() {
		System.exit(0);

	}

	private void setUpComponent() {
		// 按钮
		button1 = new JButton("登录");
		button2 = new JButton("退出");
		// 标签
		label1 = new JLabel("用户名:");
		label2 = new JLabel("密   码:");
		/*label2.addKeyListener(this);*/
		// 文本框
		jText1 = new JTextField(10);

		jpassword = new JPasswordField(10);
		jpassword.addKeyListener(this);//给密码框绑定键盘回车点击事件
		frame.add(button1);
		frame.add(button2);
		frame.add(label1);
		frame.add(label2);
		frame.add(jText1);
		frame.add(jpassword);
		Container contentPane = frame.getContentPane();

		contentPane.setLayout(new BorderLayout());
		JPanel p = new JPanel(new GridLayout(3, 1));
		JPanel p1 = new JPanel(new FlowLayout());
		JPanel p2 = new JPanel(new FlowLayout());
		JPanel p3 = new JPanel(new FlowLayout());

		p1.add(button1);
		p1.add(button2);
		p2.add(label1);
		p2.add(jText1);
		p3.add(label2);
		p3.add(jpassword);

		contentPane.add(p1, BorderLayout.SOUTH);
		contentPane.add(p2, BorderLayout.NORTH);
		contentPane.add(p3, BorderLayout.CENTER);
	}
	public static void main(String[] args) {
		new Salesmen();
	}

	public void login_method(){
		//IWorkDao dao = (IWorkDao) BeanFactory.getPojo("workDao");
		IWorkDao dao=new WorkDaoImp();
		try {
			WorkMen work = dao.find(jText1.getText());//通过输入的姓名去数据库查他的用户名与密码是否一一对应
			//如果一一对应的话，则返回work对象，否则返回null
			// System.out.println(work);
			System.out.println(jText1.getText());
			System.out.println(jpassword.getPassword());
			System.out.println(work.getPassword());
			if (work == null) {
				JOptionPane.showMessageDialog(null, "用户名不存在", "消息框",
						JOptionPane.OK_OPTION);
			} else if (!jpassword.getText().trim().equals(//trim函数是为了去掉前面与后面的空格
					work.getPassword())) {//输入的密码与用户数据库的密码相匹配，如果密码不一致的话
				JOptionPane.showMessageDialog(null, "密码不正确", "消息框",
						JOptionPane.OK_OPTION);
			} else {//用户存在且密码一致，则跳进下一步，输入服务台编号
				try {
					String panel = JOptionPane.showInputDialog(null,
							"输入服务台编号", "请输入",
							JOptionPane.DEFAULT_OPTION);
					System.out.println(panel + "*1111");
					if (panel == null || panel.trim().length() == 0) {//如果没输入酒店及确定，则提示输入不能为空
						// if(JOptionPane.OPTION_TYPE_PROPERTY==JOptionPane.YES_NO_OPTION)
						JOptionPane.showMessageDialog(null, "不能为空",
								"消息框", JOptionPane.OK_OPTION);
					} else {//输入数字，且数字为1或2

						int num = Integer.valueOf(panel);
						System.out.println("the number is"+num);
						// JOptionPane.showMessageDialog(null,"不能为空","消息框",JOptionPane.OK_OPTION);
						new ProcessClient(num);//执行处理顾客程序

						frame.dispose();
					}
				} catch (Exception e1) {
					e1.getMessage();
				}
			}
		} catch (Exception e1) {
			e1.getMessage();
		}
	}


	@Override
	public void keyTyped(KeyEvent e) {

	}

	@Override
	public void keyPressed(KeyEvent e) {
		//回车键
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			login_method();
		}

	}

	@Override
	public void keyReleased(KeyEvent e) {

	}
}
